package kz.asoft;

import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import kz.asoft.am.common.RisksAppModule;
import kz.asoft.utils.ADFUtils;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.layout.RichShowDetailItem;

import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
import oracle.adf.view.rich.component.rich.nav.RichCommandToolbarButton;
import oracle.adf.view.rich.render.ClientEvent;

import oracle.jbo.ViewObject;

import org.apache.myfaces.trinidad.event.DisclosureEvent;


public class QuasiManageBean {
    private RichShowDetailItem showDetailsItem;

    public QuasiManageBean() {
    }

    /**
     * Сохранение информации по 
     * квазигосударственным субъектам
     * @param actionEvent
     */
    public void saveFilledDatas(ActionEvent actionEvent) {
        RisksAppModule appModule = (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        appModule.saveDataValue(null);
        
        ViewObject rDataPeriodVO = appModule.findViewObject("RDataPeriodView1");
        rDataPeriodVO.setWhereClause(null);
        rDataPeriodVO.executeQuery();
        
        resetQuasiDataType(actionEvent);
    }
    
    /**
     * Редактирование информации по
     * квазигосудартсвенным субъектам
     * @param actionEvent
     */
    public void saveEditedFilledDatas(ActionEvent actionEvent) {
        RisksAppModule appModule = (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        appModule.saveEditedFilledDatas(null);
        
        ViewObject rDataPeriodVO = appModule.findViewObject("RDataPeriodView1");
        rDataPeriodVO.setWhereClause(null);
        rDataPeriodVO.executeQuery();
        
        resetQuasiDataType(actionEvent);
    }

    public void setShowDetailsItem(RichShowDetailItem showDetailsItem) {
        this.showDetailsItem = showDetailsItem;
    }

    public RichShowDetailItem getShowDetailsItem() {
        return showDetailsItem;
    }

    /**
     * Загрузка информации по переключению табов
     * типов информации по квазигосударственным
     * субъектам
     * @param disclosureEvent
     */
    public void disclosureListener(DisclosureEvent disclosureEvent) {
        RichShowDetailItem item = (RichShowDetailItem) disclosureEvent.getSource();
        if (item.isDisclosed()) {
            Number typeId = (Number) showDetailsItem.getAttributes().get("typeId");
            DCIteratorBinding dcIb = ADFUtils.findIterator("SPublicDataTypeView1Iterator");
            dcIb.setCurrentRowWithKeyValue(typeId.toString());
        }
    }
    
    /**
     * переключение табов 
     * типов информации по квази. субъектам
     * при редактировании
     * @param disclosureEvent
     */
    public void reloadDataListener(DisclosureEvent disclosureEvent) {
        RichShowDetailItem item = (RichShowDetailItem) disclosureEvent.getSource();
        if (item.isDisclosed()) {
            Number typeId = (Number) showDetailsItem.getAttributes().get("typeId");
            DCIteratorBinding dcIb = ADFUtils.findIterator("SPublicDataTypeView1Iterator");
            dcIb.setCurrentRowWithKeyValue(typeId.toString());
            RisksAppModule appModule = (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
            appModule.loadQuasiDataVaues();
        }
    }

    /**
     * Очистка форм при создании
     * нового периода по квази. суб.
     * @param actionEvent
     */
    public void clearInputForm(ActionEvent actionEvent) {
        RisksAppModule appModule = (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        ViewObject inputFrom = appModule.findViewObject("SPublicDataView1"); 
        inputFrom.clearCache();
    }

    /**
     * Сброс итератора типов по квази. суб.
     * на начало
     * @param actionEvent
     */
    public void resetQuasiDataType(ActionEvent actionEvent) {
        RisksAppModule appModule = (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        ViewObject quasiTypeVO = appModule.findViewObject("SPublicDataTypeView1");
        quasiTypeVO.setCurrentRow(quasiTypeVO.first());
    }

    public void quasiTableDblClick(ClientEvent clientEvent) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        UIViewRoot root = facesContext.getViewRoot();
        RichCommandButton button = (RichCommandButton)root.findComponent("r1:cmi2");
        if (button != null) {
            ActionEvent actionEvent = new ActionEvent(button);
            actionEvent.queue();
        }
    }
    
    public void dataTableDblClick(ClientEvent clientEvent) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        UIViewRoot root = facesContext.getViewRoot();
        RichCommandButton button = (RichCommandButton)root.findComponent("r1:cmi25");
        if (button != null) {
            ActionEvent actionEvent = new ActionEvent(button);
            actionEvent.queue();
        }
    }
}
